package com.ys.service.finance.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ys.enume.base.TransactionTypeEnum;
import com.ys.mapper.finance.TcwOperateFrozenRunningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwAccountCountForCompany;
import com.ys.entity.finance.TcwDepositPlatform;
import com.ys.entity.finance.TcwOperateFrozenRunning;
import com.ys.entity.finance.TcwPaymentReturnItem;
import com.ys.entity.finance.TcwWanliFinance;
import com.ys.entity.finance.TcwWanliPaymentReturn;
import com.ys.entity.finance.TcwWanliPaymentReturnItem;
import com.ys.entity.finance.TcwWanliTransfer;
import com.ys.entity.flow.YspModel;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.finance.PaymentEnum;
import com.ys.enume.finance.TcwOperateFrozenRunningEnum;
import com.ys.enume.flow.FlowEnum;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.finance.TcwAccountCountForCompanyMapper;
import com.ys.mapper.finance.TcwPaymentReturnItemMapper;
import com.ys.mapper.finance.TcwWanliPaymentReturnItemMapper;
import com.ys.mapper.finance.TcwWanliPaymentReturnMapper;
import com.ys.mapper.finance.TcwWanliTransferMapper;
import com.ys.service.business.TcwAccountDetailedForCompanyService;
import com.ys.service.finance.TcwAccountCountForWanliService;
import com.ys.service.finance.TcwWanliPaymentReturnService;
import com.ys.service.finance.TcwWanliTransferService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;

@Service("TcwWanliPaymentReturnService")
public class TcwWanliPaymentReturnServiceImpl implements TcwWanliPaymentReturnService {


    @Autowired
    private TcwWanliPaymentReturnMapper tcwWanliPaymentReturnMapper;

    @Autowired
    private TcwWanliPaymentReturnItemMapper tcwWanliPaymentReturnItemMapper;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    private DiySqlMapper diySqlMapper;

    @Autowired
    private TcwAccountCountForWanliService tcwAccountCountForWorldFirstService;

    @Autowired
    private TcwWanliTransferService tcwWanliTransferService;

    @Autowired
    private TcwAccountDetailedForCompanyService tcwAccountDetailedForCompanyService;

    @Autowired
    private TcwPaymentReturnItemMapper tcwPaymentReturnItemMapper;

    @Autowired
    private TcwAccountCountForCompanyMapper tcwAccountCountForCompanyMapper;

    @Autowired
    private TcwOperateFrozenRunningMapper tcwOperateFrozenRunningMapper;

    @Autowired
    private TcwWanliTransferMapper tcwWanliTransferMapper;


    /**
     * 新增万里汇预付款退回
     * @param params
     * @param subData
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R add(Map<String, Object> params, String subData) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwWanliPaymentReturn tcwWanliPaymentReturn = JSON.parseObject(JSON.toJSONString(params), TcwWanliPaymentReturn.class);
        if (new TcwWanliPaymentReturn().setBid(tcwWanliPaymentReturn.getBid()).count() != 0){
            return R.error("该单号已存在，核实后再操作!");
        }
        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }

        String loginUserId = ShiroUtils.getUserId();
        TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();

        tcwWanliPaymentReturn.setStatus(0);
        tcwWanliPaymentReturn.setCreateChangeId(String.valueOf(tsUserChange.getId()));
        tcwWanliPaymentReturn.setEffectStock(FlowEnum.EffectStock.NOT_EFFECT.value);
        tcwWanliPaymentReturnMapper.insert(tcwWanliPaymentReturn);


        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            if (detailJO.get("depositPlatformAccountCode") == null){
                throw new RuntimeException("子账号名称为空,请核实后再操作!");
            }
            TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem = JSON.parseObject(JSON.toJSONString(detailJO), TcwWanliPaymentReturnItem.class);
            if (new TcwWanliPaymentReturnItem().setPaymentReturnItemId(tcwWanliPaymentReturnItem.getPaymentReturnItemId()).count() != 0){
                return R.error("该预付款退回申请单详情已制单，核实后再操作!");
            }
            tcwWanliPaymentReturnItem.setWanliPaymentReturnBid(tcwWanliPaymentReturn.getBid());
            tcwWanliPaymentReturnItem.setStatePay(PaymentEnum.StatePay.INIT.value);
            tcwWanliPaymentReturnItemMapper.insert(tcwWanliPaymentReturnItem);
        }
        YsMenu ysMenu = new YsMenu().setId(MenuId.TCW_WanliPaymentReturn).queryFirst();
        if (ysMenu.getFlowModelId().longValue() != 0L) {
            YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
            String codeModel = yspModel.getCode();
            String createUserId = ShiroUtils.getUserId();
            yspTaskService.init(codeModel, tcwWanliPaymentReturn.getBid(), createUserId);
        }

        return R.ok(AddDto.gotoEdit("/web/finance/tcwWanliPaymentReturn/toEdit?bid=" + tcwWanliPaymentReturn.getBid()));
    }


    /**
     * 修改万里汇预付款退回
     * @param params
     * @param subData
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R edit(Map<String, Object> params, String subData) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwWanliPaymentReturn tcwWanliPaymentReturn = JSON.parseObject(JSON.toJSONString(params), TcwWanliPaymentReturn.class);
        if (new TcwWanliPaymentReturn().setBid(tcwWanliPaymentReturn.getBid()).where("A.id != " + tcwWanliPaymentReturn.getId()).count() != 0){
            return R.error("该单号已存在，核实后再操作!");
        }
        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }

        //判断是否实在制单中
        Integer flowState = yspTaskService.flowState(tcwWanliPaymentReturn.getBid());

        if (flowState == 2){
            return R.error("万里汇预付款退回单已生效,不允许保存!");
        }

        tcwWanliPaymentReturnMapper.update(tcwWanliPaymentReturn);


        //查询之前的数据
        Boolean flaxNew = null;
        List<TcwWanliPaymentReturnItem> tcwWanliPaymentReturnItemList = new TcwWanliPaymentReturnItem().setWanliPaymentReturnBid(tcwWanliPaymentReturn.getBid()).queryList();
        for (TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem : tcwWanliPaymentReturnItemList){
            for (int i = 0; i < detailJA.size(); i++) {
                JSONObject detailJONew = detailJA.getJSONObject(i);
                flaxNew = true;
                if (tcwWanliPaymentReturnItem.getId().equals(Long.valueOf(detailJONew.get("id").toString()))){
                    flaxNew = false;
                    break;
                }
            }
            if (flaxNew){
                if (tcwWanliPaymentReturnItem.getStatePay() == PaymentEnum.StatePay.PROCESS.value || tcwWanliPaymentReturnItem.getStatePay() == PaymentEnum.StatePay.SUCCESS.value){
                    //修改TCW_WanliTransfer数据
                    TcwWanliTransfer tcwWanliTransfer = new TcwWanliTransfer().setRelativeId(tcwWanliPaymentReturnItem.getId()).queryFirst();
                    if (tcwWanliTransfer != null){
                        tcwWanliTransfer.setStatus(0);
                        tcwWanliTransferMapper.update(tcwWanliTransfer);
                    }

                    //回填预付款退回支付状态
                    TcwPaymentReturnItem tcwPaymentReturnItem = new TcwPaymentReturnItem().setId(tcwWanliPaymentReturnItem.getPaymentReturnItemId()).queryFirst();
                    if (tcwPaymentReturnItem != null){
                        tcwPaymentReturnItem.setStatus(0);
                        tcwPaymentReturnItemMapper.update(tcwPaymentReturnItem);
                    }
                }

                tcwWanliPaymentReturnItemMapper.deleteTrue(tcwWanliPaymentReturnItem.getId());
            }
        }

        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            if (detailJO.get("depositPlatformAccountCode") == null){
                throw new RuntimeException("子账号名称为空,请核实后再操作!");
            }
            TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem = JSON.parseObject(JSON.toJSONString(detailJO), TcwWanliPaymentReturnItem.class);
            if (new TcwWanliPaymentReturnItem().setPaymentReturnItemId(tcwWanliPaymentReturnItem.getPaymentReturnItemId()).where("A.id != " + tcwWanliPaymentReturnItem.getId()).count() != 0){
                throw new RuntimeException("该预付款退回申请单详情已制单，核实后再操作!");
            }
            if (tcwWanliPaymentReturnItem.getId() != null ){
                tcwWanliPaymentReturnItemMapper.update(tcwWanliPaymentReturnItem);
            }else {
                tcwWanliPaymentReturnItem.setWanliPaymentReturnBid(tcwWanliPaymentReturn.getBid());
                tcwWanliPaymentReturnItem.setStatePay(PaymentEnum.StatePay.INIT.value);
                tcwWanliPaymentReturnItemMapper.insert(tcwWanliPaymentReturnItem);
            }
        }
        return R.ok(AddDto.gotoEdit("/web/finance/tcwWanliPaymentReturn/toEdit?bid=" + tcwWanliPaymentReturn.getBid()));
    }

    /**
     * 万里汇预付款退回流转
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R flowPass(Long id, String loginUserId) throws Exception {
        TcwWanliPaymentReturn entity = new TcwWanliPaymentReturn().setId(id).queryFirst();
        String bid = entity.getBid();
        try {
            Flow f = yspTaskService.flowPass(bid, loginUserId);
            if (f.getCode() == Flow.Code.FAILED.value) {
                return R.error(f.getMsg());
            } else {
                if (f.getState() == Flow.State.FINISH.value) {
                    R r = flowFinish(entity);
                    if (r.getCode() == 1) {
                        return R.error(r.getMsg());
                    }
                }
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 删除万里汇预付款退回
     * @param id 主键id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R del(Long id) throws Exception {
        TcwWanliPaymentReturn tcwWanliPaymentReturn = new TcwWanliPaymentReturn().setId(id).queryFirst();
        if (tcwWanliPaymentReturn == null){
            throw new RuntimeException("万里汇预付款退回单据不存在,请核实后再操作!");
        }

        //判断是否实在制单中
        Integer flowState = yspTaskService.flowState(tcwWanliPaymentReturn.getBid());

        if (flowState != 0){
            throw new RuntimeException("万里汇预付款退回单不是制单中,不允许删除!");
        }

        yspTaskService.flowDelete(tcwWanliPaymentReturn.getBid(),tcwWanliPaymentReturn.getCreateChangeId());

        tcwWanliPaymentReturnMapper.deleteTrue(id);

        diySqlMapper.deleteTcwWanliPaymentReturnBid(tcwWanliPaymentReturn.getBid());

        return R.ok("删除成功!");
    }



    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+ MenuId.TCW_WanliPaymentReturn)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TCW_WanliPaymentReturn",
                    listField,
                    infoMap,
                    ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Transactional
    public R flowFinish(TcwWanliPaymentReturn entity) throws Exception {

        List<TcwWanliPaymentReturnItem> tcwWanliPaymentReturnList = new TcwWanliPaymentReturnItem().setWanliPaymentReturnBid(entity.getBid()).queryList();

        List<TcwWanliPaymentReturnItem> tcwWanliPaymentReturnListNew;

        tcwWanliPaymentReturnListNew = tcwWanliPaymentReturnList.stream().filter(
                tcwWanliPaymentReturnItem -> tcwWanliPaymentReturnItem.getServiceCharge() == null
                        || tcwWanliPaymentReturnItem.getZfAmount() == null
                        || tcwWanliPaymentReturnItem.getExchangeRate() == null
        ).collect(Collectors.toList());
        if (tcwWanliPaymentReturnListNew.size() != 0){
            throw new RuntimeException("单据中支付币种金额/汇率/手续费数据为空,请核实后再操作!");
        }

        tcwWanliPaymentReturnListNew = tcwWanliPaymentReturnList.stream().filter(
                tcwWanliPaymentReturnItem -> tcwWanliPaymentReturnItem.getStatePay() != PaymentEnum.StatePay.SUCCESS.value
        ).collect(Collectors.toList());
        if (tcwWanliPaymentReturnListNew.size() != 0){
            throw new RuntimeException("单据中存在已支付以外数据,请核实后再操作!");
        }

        for (TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem : tcwWanliPaymentReturnList){
            //只有已支付的才通过流水
            if (tcwWanliPaymentReturnItem.getStatePay() == PaymentEnum.StatePay.SUCCESS.value){
                payZrSuccess(tcwWanliPaymentReturnItem.getId());
            }
        }
        entity.setEffectStock(1);
        tcwWanliPaymentReturnMapper.update(entity);
        return R.ok();
    }

    @Override
    public void paySuccess(Long id) throws Exception {
        TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem = new TcwWanliPaymentReturnItem().setId(id).queryFirst();
        tcwWanliPaymentReturnItem.setStatePay(PaymentEnum.StatePay.SUCCESS.value);
        tcwWanliPaymentReturnItemMapper.update(tcwWanliPaymentReturnItem);

        //预付款退回
        TcwPaymentReturnItem tcwPaymentReturnItem = new TcwPaymentReturnItem().setId(tcwWanliPaymentReturnItem.getPaymentReturnItemId()).queryFirst();

        tcwPaymentReturnItem.setStatus(PaymentEnum.StatePay.SUCCESS.value);
        tcwPaymentReturnItem.setExecutionTime(new Date());
        tcwPaymentReturnItemMapper.update(tcwPaymentReturnItem);
    }

    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public void payZrSuccess(Long id) throws Exception {
        TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem = new TcwWanliPaymentReturnItem().setId(id).queryFirst();
        tcwWanliPaymentReturnItem.setStatePay(PaymentEnum.StatePay.SUCCESS.value);
        tcwWanliPaymentReturnItemMapper.update(tcwWanliPaymentReturnItem);

        //主表数据
        TcwWanliPaymentReturn tcwWanliPaymentReturn = new TcwWanliPaymentReturn().setBid(tcwWanliPaymentReturnItem.getWanliPaymentReturnBid()).queryFirst();

        //预付款退回
        TcwPaymentReturnItem tcwPaymentReturnItem = new TcwPaymentReturnItem().setId(tcwWanliPaymentReturnItem.getPaymentReturnItemId()).queryFirst();

        tcwPaymentReturnItem.setStatus(PaymentEnum.StatePay.SUCCESS.value);
        tcwPaymentReturnItem.setExecutionTime(new Date());
        tcwPaymentReturnItemMapper.update(tcwPaymentReturnItem);

        //查询运营公司银行账户
        ThrOperateCompanyBankInfo thrOperateCompanyBankInfo  = new ThrOperateCompanyBankInfo().setId(tcwPaymentReturnItem.getCompanyBankId()).queryFirst();

        //运营公司简称
        ThrOperateCompanyApply thrOperateCompanyApply = new ThrOperateCompanyApply().setVgsbm(thrOperateCompanyBankInfo.getVgsbm()).queryFirst();

        //查询收款平台账户
        TcwDepositPlatform tcwDepositAccount = new TcwDepositPlatform().setId(tcwWanliPaymentReturn.getDepositPlatformId()).queryFirst();
        if (tcwDepositAccount == null){
            throw new RuntimeException("收款平台账户不存在!");
        }

        //查询万里汇账户资金
        TcwWanliFinance tcwWorldFirstFinance = new TcwWanliFinance().setDepositPlatformCode(tcwDepositAccount.getCode()).queryFirst();
        if (tcwWorldFirstFinance == null){
            throw new RuntimeException("万里汇账户资金不存在!");
        }

        //支付流水
        R add = tcwAccountCountForWorldFirstService.add(false, tcwDepositAccount.getCode(),
                tcwWanliPaymentReturn.getCurrencyCode(), 3,tcwWanliPaymentReturn.getBid(), tcwWanliPaymentReturnItem.getZfAmount().negate()
                , "单号:" + tcwWanliPaymentReturn.getBid() + ",本单备注:" + tcwWanliPaymentReturn.getVbz() + ",汇率:" + tcwWanliPaymentReturnItem.getExchangeRate());
        if (add.getCode() == 1){
            throw new RuntimeException(add.getMsg());
        }

        //运营流水
        /**
         * 新增一条流水
         * 必填：vgsbm划出公司编码；vgsbmp划入公司编码；vjylx交易类型；fje交易金额；djysj交易时间；dshsj审核时间；vbz备注
         * 选填：mname;mtype;mnid;mbid;mguid；writeTime记账时间（如果为空，默认审核时间）
         */
        tcwAccountDetailedForCompanyService.addTcwAccountDetailedForCompanyWithJcNoTrigger(
                thrOperateCompanyBankInfo.getVgsbm(),thrOperateCompanyApply.getVgsjc(),thrOperateCompanyBankInfo.getVyhzh(),thrOperateCompanyBankInfo.getVyhzhmc(),
                tcwPaymentReturnItem.getVJyLx(), tcwWanliPaymentReturnItem.getThAmount().negate(),tcwWanliPaymentReturnItem.getCreateTime(), new Date(),
                "预付款退回申请："+tcwWanliPaymentReturn.getBid()+",预付款退回:"+tcwPaymentReturnItem.getPaymentReturnBid() + ",本单备注:"+tcwWanliPaymentReturn.getVbz(),
                tcwWanliPaymentReturn.getBid(),null);


        //付款币种为CNH时，需要产生一个业务交易流水；
        if(tcwWanliPaymentReturn.getCurrencyCode().equals("CNH") && tcwWanliPaymentReturnItem.getServiceCharge()!=null && tcwWanliPaymentReturnItem.getServiceCharge().compareTo(BigDecimal.ZERO)>0){
            tcwAccountDetailedForCompanyService.addTcwAccountDetailedForCompanyWithJcNoTrigger(
                    "GS0889", "问前集团",
                    "HZZH0155", "万里汇",
                    TransactionTypeEnum.BusinessType.CWFY_SXF.name,
                    BigDecimal.ZERO.subtract(tcwWanliPaymentReturnItem.getServiceCharge()),
                    tcwWanliPaymentReturnItem.getCreateTime(), new Date(),
                    "预付款退回申请："+tcwWanliPaymentReturn.getBid()+",预付款退回:"+tcwPaymentReturnItem.getPaymentReturnBid() + ",本单备注:"+tcwWanliPaymentReturn.getVbz(),
                    tcwWanliPaymentReturn.getBid(),null
            );

        }


        //根据公司编码获取对应的实时资金表信息
        TcwAccountCountForCompany account = new TcwAccountCountForCompany().setVGsBm(thrOperateCompanyBankInfo.getVgsbm()).queryFirst();

        account.setFDjJe(account.getFDjJe().subtract(tcwPaymentReturnItem.getThAmount()));
        //回填冻结金额
        tcwAccountCountForCompanyMapper.update(account);

        tcwOperateFrozenRunningMapper.insert(new TcwOperateFrozenRunning().setSourceId(id)
                .setChangeFdjje(tcwPaymentReturnItem.getThAmount())
                .setFdjje(account.getFDjJe())
                .setVgsbm(thrOperateCompanyBankInfo.getVgsbm())
                .setSourceType(TcwOperateFrozenRunningEnum.DocumentType.TCWWANLIPAYMENTRETURNITEM.value)
                .setRemark("万里汇预付款退回流转生效,账户资金:"+account.getFJe()+",负债额度:"+account.getFFzEd()));
    }



    @Override
    public R zrWanli(String id) throws Exception {
        TcwWanliPaymentReturnItem tcwWanliPaymentReturnItem = new TcwWanliPaymentReturnItem().setId(Long.valueOf(id)).queryFirst();
        if (tcwWanliPaymentReturnItem == null){
            return R.error("该数据不存在,不允许操作!");
        }
        TcwWanliPaymentReturn tcwWanliPaymentReturn = new TcwWanliPaymentReturn().setBid(tcwWanliPaymentReturnItem.getWanliPaymentReturnBid()).queryFirst();

        Integer state = yspTaskService.flowState(tcwWanliPaymentReturn.getBid());
        if (state == Flow.State.INIT.value) {
            return R.error("该单据还未流转,不允许操作!");
        }
        if (tcwWanliPaymentReturn.getEffectStock().equals(1)){
            return R.error("该单据已生效,不允许操作!");
        }

        if (tcwWanliPaymentReturnItem.getZfAmount() == null ){
            return R.error("支付金额不能为空,不允许操作!");
        }
        if (tcwWanliPaymentReturnItem.getZfAmount().equals("")){
            return R.error("支付金额不能为空,不允许操作!");
        }

        if (tcwWanliPaymentReturnItem.getStatePay().equals(PaymentEnum.StatePay.INIT.value) || tcwWanliPaymentReturnItem.getStatePay().equals(PaymentEnum.StatePay.FAILED.value)){
            //判断是否重复
            if (new TcwWanliTransfer().setRelativeId(tcwWanliPaymentReturnItem.getId()).setState(1).count() != 0){
                return R.error("该单正在执行支付万里汇,请勿重复支付!");
            }
            tcwWanliTransferService.addPaymentReturnItem(Long.valueOf(id));
        }
        return R.ok("操作成功");
    }

    //获取列表数据
    @Override
    @Transactional
    public Map<String,Object> getExtraParam( String bid) throws Exception{
        Map<String,Object> entity = new TcwWanliPaymentReturn().setBid(bid)
                .addLeftJoin("YS_ProcInst Ysp ON Ysp.bid = A.bid")//流程表
                .addField("FORMAT(Ysp.UpdateTime, 'yyyy-MM-dd hh:mm:ss') AS updateTime1")//审核时间
                .addLeftJoin("TCW_DepositPlatform tda ON tda.id = A.DepositPlatformId")//收款账户平台
                .addField("tda.id AS depositPlatformId,tda.platformName AS platformName,tda.code AS depositPlatformCode,tda.name AS depositPlatformName")
                .addLeftJoin("TB_CompanyInfo tci ON tci.vid = tda.CompanyVid ")//公司
                .addField("tci.vname AS companyName")
                .queryFirstMap();
        entity.remove("createTime");
        return entity;
    }
    //获取子列表数据
    @Override
    @Transactional
    public List<Map<String, Object>> getItemList( String bid) throws Exception{
        List<Map<String, Object>> sonlist = new TcwWanliPaymentReturnItem()
                .setWanliPaymentReturnBid(bid)
                .addLeftJoin("TCW_PaymentReturnItem tpr ON tpr.id = A.PaymentReturnItemId")//预付款申请详情
                .addField("tpr.paymentReturnBid AS paymentReturnBid, tpr.vJyLx AS vJyLx,tpr.accountType AS accountType")
                .addLeftJoin("THR_OperateCompanyBankInfo ocbi ON ocbi.id = tpr.companyBankId")//银行账户
                .addField("ocbi.vyhzhmc AS vyhzhmc,ocbi.vkhh AS vkhh,ocbi.vyhzh AS vyhzh,ocbi.vlhh AS vlhh")
                .addLeftJoin("THR_OperateCompanyApply THRO ON THRO.vgsbm = ocbi.vgsbm")//运营公司
                .addField("THRO.vgsjc AS vgsjc,THRO.vgsbm AS vgsbm")
                .addField("(SELECT top 1 tdpa.Vyhzhmc  FROM TCW_DepositPlatformAccount tdpa WHERE tdpa.Vgsbm = THRO.Vgsbm) AS depositPlatformAccountCode")
                .queryMap();
        return sonlist;
    }

}
